package com.sumainfor.pc.market.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.feign.BaseFeignGmio;
import com.sumainfor.pc.market.service.HousingService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 业主提前收房
 *
 * @Auther: zhlu
 * @Date: 2019/1/12 16:06
 * @Description:
 */
@RestController
@RequestMapping("/houow")
public class HousingOwadvanceController {

    Logger log = LoggerFactory.getLogger(HousingOwadvanceController.class);

    @Autowired
    BaseFeignGmio BaseFeignGmio;

    @Autowired
    HousingService housingService;

    /**
     * 功能描述:
     * 获取业主提前收房列表
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/1/12 16:17
     */
    @ResponseBody
    @GetMapping("/getHousingOwList")
    public JsonResult getHousingOwList(@RequestParam Map<String, Object> params, Pager pager) {
        JsonResult result = new JsonResult();
        JsonResult HousingOw = BaseFeignGmio.getHousingOwList(params, pager);
        if (HousingOw.isSuccess() == true) {
            Map<String, Object> Housing = (Map<String, Object>) HousingOw.getResult();
            Map<String, Object> getHousingOwLists = housingService.getHousingOwLists(Housing);
            result.put(getHousingOwLists);
        } else {
            return HousingOw;
        }
        return result;
    }

    /**
     * 功能描述:
     * 业主放弃提前收房
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/1/15 20:00
     */
    @ResponseBody
    @PostMapping("/UpdateHousingOwGiveUp")
    public JsonResult UpdateHousingOwGiveUp(@RequestBody Map<String, Object> params) {
        JsonResult result = new JsonResult();
        result = BaseFeignGmio.UpdateHousingOwGiveUp(params);
        return result;
    }

    /**
     * 功能描述:
     * 发起违约单
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/1/16 15:33
     */
    @ResponseBody
    @PostMapping("/AddHousingOwInfo")
    public JsonResult AddHousingOwInfo(@RequestBody Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"HouId"})) {
            result.putFailed("HouId不能为空！");
            return result;
        }
        String HouId = MapUtils.getString(params, "HouId");
        //如果在租租客合同大于0，不容许发起业主违约
        if (housingService.getHouIdByRenting(params) > 0) {
            result.putFailed("房源存在租赁合同！");
            return result;
        }
        //房源数据修改失败
        if (!housingService.ownerHouse(HouId)) {
            result.putFailed("房源数据修改失败！");
            return result;
        }
        //获取业主资料
        Map<String, Object> getHouIdByOwner = housingService.getHouIdByOwner(params);
        params.putAll(getHouIdByOwner);
        result = BaseFeignGmio.AddHousingOwInfo(params);
        return result;
    }

    /**
     * 功能描述:
     * 根据房源编号查询业主成员变更记录
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/1/17 17:08
     */
    @ResponseBody
    @GetMapping("/getHouOwmemberList")
    public JsonResult getHouOwmemberList(@RequestParam Map<String, Object> params, Pager pager) {
        JsonResult result = new JsonResult();
        result = BaseFeignGmio.getHouOwmemberList(params, pager);
        return result;
    }

    /**
     * 功能描述:
     * 根据房源编号查询业主收款人变更列表
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/1/17 17:08
     */
    @ResponseBody
    @GetMapping("/getHouOwPayeeList")
    public JsonResult getHouOwPayeeList(@RequestParam Map<String, Object> params, Pager pager) {
        JsonResult result = new JsonResult();
        result = BaseFeignGmio.getHouOwPayeeList(params, pager);
        return result;
    }

}
